package tel.tool;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;

import tel.tool.model.ResultA;

public class HttpClient {
	public static String get(String uri) throws ClientProtocolException, IOException {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(uri);
		System.out.println("Executing request " + httpGet.getRequestLine());
		try {
			// Create a custom response handler
			ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
				@Override
				public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						HttpEntity entity = response.getEntity();
						return entity != null ? EntityUtils.toString(entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}

			};
			String responseBody = httpClient.execute(httpGet, responseHandler);
//			System.out.println("----------------------------------------");
//			System.out.println(responseBody);
			return responseBody;
		} finally {
			httpClient.close();
		}
	}

	public static void main(String[] args) throws ClientProtocolException, IOException {
		String result = HttpClient.get("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13800138000");
		String json = result.split("=")[1];
		System.out.println(json);
		ResultA result2 = JSON.parseObject(json,ResultA.class);
//		System.err.println(result2.getProvince());

	}
	class Result{
		private String mts;
		private String province;
		private String catName;
		private String telString;
		private String areaVid;
		private String ispVid;
		private String carrier;
		public String getMts() {
			return mts;
		}
		public void setMts(String mts) {
			this.mts = mts;
		}
		public String getProvince() {
			return province;
		}
		public void setProvince(String province) {
			this.province = province;
		}
		public String getCatName() {
			return catName;
		}
		public void setCatName(String catName) {
			this.catName = catName;
		}
		public String getTelString() {
			return telString;
		}
		public void setTelString(String telString) {
			this.telString = telString;
		}
		public String getAreaVid() {
			return areaVid;
		}
		public void setAreaVid(String areaVid) {
			this.areaVid = areaVid;
		}
		public String getIspVid() {
			return ispVid;
		}
		public void setIspVid(String ispVid) {
			this.ispVid = ispVid;
		}
		public String getCarrier() {
			return carrier;
		}
		public void setCarrier(String carrier) {
			this.carrier = carrier;
		}
	}
}
